//
//  AppDelegate.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/8.
//  Copyright © 2016年 itheima. All rights reserved.


import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //0.设置全局皮肤.
        stupAppearance()
    
        // 1,创建窗口.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 2,设置窗口的根控制器.
        window?.rootViewController = getWindowRootVC()
        
        //TODO: 测试欢迎控制器和新界面控制器.
//        window?.rootViewController = TJWelcomeViewController()
//        window?.rootViewController = TJNewFeatureViewController()
        
//        TJPrint("是否新版本:\(isNewVersion())")
     
        // 3,显示主窗口.
        window?.makeKeyAndVisible()
        
        return true
    }
    
    // MARK; 切换窗口的根控制器.
    func changeWindowRootVC(controller:UIViewController) {
    
        window?.rootViewController = controller
    }
    
    //MARK: 根据逻辑判断选择需要显示的窗口的根控制器.
    private func getWindowRootVC()->UIViewController {
    
        // 先判断是否登录.
        if TJAccountViewModel.shareUserAccount.isUserAccountLogin {
        
            // 用户已经登录.在判断是否是新版本.
            return isNewVersion() ? TJNewFeatureViewController() : TJWelcomeViewController()
        }
          // 用户未登录.
        return TJTabBarController()
    }
    
    // MARK - 判断是否是新版本.
    private func isNewVersion()-> Bool {
        
        // 1,获得当前版本.
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 2,获得沙盒中的版本.
        let versionKey = "appVersion"
        let bundleVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey)
        // 3,比较.
        if currentVersion != bundleVersion {
            
            // 保存新版本.
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
            NSUserDefaults.standardUserDefaults().synchronize()
            return true
        }
        return false
    }
    
     // MARK - 设置UIBarButtonItem的全局皮肤.
    private func stupAppearance() {
    
        // H获取全局barButton
        let navBarBtnAp:UIBarButtonItem = UIBarButtonItem.appearance()
//        navBarBtnAp.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Normal) //如果设置title的话,后面item在enable状态下,颜色也改变不了了.
        navBarBtnAp.tintColor = UIColor.orangeColor()
    }
    
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        // APP进入后台后,删除过期的数据.
        TJStatusDALManager.shareDALManager.deleteCacheStatus()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        
        // APP进入后台后,删除过期的数据.
        TJStatusDALManager.shareDALManager.deleteCacheStatus()
    }


}

